<?php
    /**
     *  
     */
    class BlogArticleComment extends BaseModel
    {

        CONST ISPILLOWTALK_ALL      = -1;
        CONST ISPILLOWTALK_OPEN     = 1;
        CONST ISPILLOWTALK_PRIVATE  = 2;

        protected $_id;
        protected $_parentId;
        protected $_blogId;
        protected $_articleId;
        protected $_topic;
        protected $_text;
        protected $_userId;
        protected $_clientName;
        protected $_clientIp;
        protected $_clientIpn;
        protected $_isPillowTalk;
        protected $_updateDate;
        protected $_properties;
        protected $_normalizedSearch;

        /**
         *  讀取資料表而產生, 請視同cache, 任何情況下皆不可更動該產生的內容
         *  @return Array()
         */
        public static function getTableDefinition()
        {
            return Array();
        }

        /**
         *  經資料表傳至程式中時所對應的 method 資訊
         *  @return Array()
         */
        public static function getTableFields()
        {
            return Array(
                "id"                    => "getId",
                "parent_id"             => "getParentId",
                "blog_id"               => "getBlogId",
                "article_id"            => "getArticleId",
                "topic"                 => "getTopic",
                "text"                  => "getText",
                "user_id"               => "getUserId",
                "client_name"           => "getClientName",
                "client_ip"             => "getClientIp",
                "client_ipn"            => "getClientIpn",
                "is_pillow_talk"        => "getIsPillowTalk",
                "update_date"           => "getUpdateDateByDb",
                "properties"            => "getProperties",
                "normalized_search"     => "getNormalizedSearch"
            );
        }

        /**
         *  reset to default value model
         *  @return Array()
         */
        public function resetValue() {
            $this->__sleep();
            $this->setId                   ( -1 );
            $this->setParentId             ( 0 );
            $this->setBlogId               ( 0 );
            $this->setArticleId            ( 0 );
            $this->setTopic                ( NULL );
            $this->setText                 ( NULL );
            $this->setUserId               ( 0 );
            $this->setClientName           ( NULL );
            $this->setClientIp             ( NULL );
            $this->setClientIpn            ( 0 );
            $this->setIsPillowTalk         ( BlogArticleComment::ISPILLOWTALK_OPEN );
            $this->setUpdateDate           ( time() );
            $this->setProperties           ( Array() );
            $this->setNormalizedSearch     ( NULL );
        }

        /**
         *  validate model data
         *  @return messages Array()
         */
        public function validate()
        {
            $validateInfo = parent::validate();

            // choose value
            $result = false;
            foreach( $this->getIsPillowTalkList('is_pillow_talk') as $name => $id ) {
                if( $this->getIsPillowTalk()==$id ) {
                    $result = true;
                    break;
                }
            }
            if(!$result) {
                $validateInfo['is_pillow_talk']['message'] = 'is_pillow_talk 不正確';
            }

            // timestamp
            if( $this->getUpdateDate() < -28800 ) {
                $validateInfo['updateDate']['message'] = '日期不正確';
            }
            
            return $validateInfo;
        }

        /**
         *  filter model data
         */
        public function filter()
        {
            $this->setParentId             ( (int) $this->getParentId() );
            $this->setBlogId               ( (int) $this->getBlogId() );
            $this->setArticleId            ( (int) $this->getArticleId() );
            $this->setTopic                ( trim(strip_tags( $this->getTopic()                )) );
            $this->setText                 ( trim(strip_tags( $this->getText()                 )) );
            $this->setUserId               ( (int) $this->getUserId() );
            $this->setClientName           ( trim(strip_tags( $this->getClientName()           )) );
            $this->setClientIp             ( trim(strip_tags( $this->getClientIp()             )) );
            $this->setClientIpn            ( (int) $this->getClientIpn() );
            $this->setIsPillowTalk         ( (int) $this->getIsPillowTalk() );
            $this->setNormalizedSearch     ( trim(strip_tags( $this->getNormalizedSearch()     )) );
        }

        /* ------------------------------------------------------------------------------------------------------------------------
            basic method 
        ------------------------------------------------------------------------------------------------------------------------ */


        /**
         *  get ParentId 
         *  @return int
         */
        public function getParentId()
        {
            return $this->_parentId;
        }
        /**
         *  set ParentId
         */
        public function setParentId( $parentId )
        {
            $this->_parentId = (int) $parentId;
        }

        /**
         *  get BlogId 
         *  @return int
         */
        public function getBlogId()
        {
            return $this->_blogId;
        }
        /**
         *  set BlogId
         */
        public function setBlogId( $blogId )
        {
            $this->_blogId = (int) $blogId;
        }

        /**
         *  get ArticleId 
         *  @return int
         */
        public function getArticleId()
        {
            return $this->_articleId;
        }
        /**
         *  set ArticleId
         */
        public function setArticleId( $articleId )
        {
            $this->_articleId = (int) $articleId;
        }

        /**
         *  get Topic
         *  @return string
         */
        public function getTopic()
        {
            return $this->_topic;
        }
        /**
         *  set Topic
         */
        public function setTopic( $topic )
        {
            $this->_topic = trim($topic);
        }

        /**
         *  get Text
         *  @return string
         */
        public function getText()
        {
            return $this->_text;
        }
        /**
         *  set Text
         */
        public function setText( $text )
        {
            $this->_text = trim($text);
        }

        /**
         *  get UserId 
         *  @return int
         */
        public function getUserId()
        {
            return $this->_userId;
        }
        /**
         *  set UserId
         */
        public function setUserId( $userId )
        {
            $this->_userId = (int) $userId;
        }

        /**
         *  get ClientName
         *  @return string
         */
        public function getClientName()
        {
            return $this->_clientName;
        }
        /**
         *  set ClientName
         */
        public function setClientName( $clientName )
        {
            $this->_clientName = trim($clientName);
        }

        /**
         *  get ClientIp
         *  @return string
         */
        public function getClientIp()
        {
            return $this->_clientIp;
        }
        /**
         *  set ClientIp
         */
        public function setClientIp( $clientIp )
        {
            $this->_clientIp = trim($clientIp);
        }

        /**
         *  get ClientIpn 
         *  @return int
         */
        public function getClientIpn()
        {
            return $this->_clientIpn;
        }
        /**
         *  set ClientIpn
         */
        public function setClientIpn( $clientIpn )
        {
            $this->_clientIpn = (int) $clientIpn;
        }

        /**
         *  get IsPillowTalk 
         *  @return int
         */
        public function getIsPillowTalk()
        {
            return $this->_isPillowTalk;
        }
        /**
         *  set IsPillowTalk
         */
        public function setIsPillowTalk( $isPillowTalk )
        {
            $this->_isPillowTalk = (int) $isPillowTalk;
        }



        /**
         *  get NormalizedSearch
         *  @return string
         */
        public function getNormalizedSearch()
        {
            return $this->_normalizedSearch;
        }
        /**
         *  set NormalizedSearch
         */
        public function setNormalizedSearch( $normalizedSearch )
        {
            $this->_normalizedSearch = trim($normalizedSearch);
        }


        /* ------------------------------------------------------------------------------------------------------------------------
            extends
        ------------------------------------------------------------------------------------------------------------------------ */
        /**
         *  clear extends information
         */
        public function __sleep()
        {
            $this->_user = null;
            $this->_blog = null;
            return parent::__sleep();
        }

        /**
         *  get user model ( 資料表必須要提供 user_id 欄位 )
         *  @param isSave 是否儲存取得的物件
         *  @return object
         */
        public function getUser( $isSave=true )
        {
            if( !$isSave ) {
                $this->_user = null;
            }
            if( $this->_user ) {
                return $this->_user;
            }

            $users = new Users();
            $user = $users->getUser( $this->getUserId() );

            if( $isSave ) {
                $this->_user = $user;
            }
            return $user;
        }


        /**
         *  get blog model
         *  @param  boolean - isSave , is cache the object
         *  @return object
         */
        public function getBlog( $isSave=true )
        {
            if( !$isSave ) {
                $this->_blog = null;
            }
            if( $this->_blog ) {
                return $this->_blog;
            }

            $blogs = new Blogs();
            $blog = $blogs->getBlog( $this->getBlogId() );

            if( $isSave ) {
                $this->_blog = $blog;
            }
            return $blog;
        }

    }
